package GUILayer;

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.Timer;

import java.util.List;
import java.awt.LayoutManager;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.ListSelectionModel;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import GUIadapter.PanelTableModelAdapter;
import GUIadapter.SaleItemModel;
import CtrlLayer.*;
import ModelLayer.SaleItem;

public class PanelLager extends JPanel implements Updateable, ActionListener
{
	private List<SaleItem> saleItemList;
	private PanelTableModelAdapter panelSaleItem;
	private int indexTableSaleItem;
	private ItemCtrl itemCtrl;
	private PopupOpretVare popupOpretVare;
	private PopupRedigerVare popupRedigerVare;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private static final long serialVersionUID = 1L;
	
	JLabel lblTime1 = new JLabel();
	SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
	Timer timer;
	
	public PanelLager() 
	{
		popupOpretVare = new PopupOpretVare();
		popupRedigerVare = new PopupRedigerVare();
		
		JRadioButton rdbtnTraelast = new JRadioButton("Tr\u00E6last");
		buttonGroup.add(rdbtnTraelast);
		
		JRadioButton rdbtnByggemarked = new JRadioButton("Byggemarked");
		buttonGroup.add(rdbtnByggemarked);
		
		JButton btnOpret = new JButton("Opret ny vare");
		btnOpret.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				popupOpretVare.setVisible(true);
			}
		});
		btnOpret.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				if ((indexTableSaleItem >= 0) && (indexTableSaleItem < saleItemList.size()))
				{
					update();
				}
			}
		});
		btnOpret.setHorizontalAlignment(SwingConstants.LEFT);
		
		JButton btnRediger = new JButton("Rediger vare");
		btnRediger.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				popupRedigerVare.setVisible(true);
			}
		});
		btnRediger.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				if ((indexTableSaleItem >= 0) && (indexTableSaleItem < saleItemList.size()))
				{
					update();
				}
			}
		});
		
		JButton btnSlet = new JButton("Slet vare");
		
		
		JButton btnUdskriv = new JButton("Udskriv faktura");
		
		itemCtrl = new ItemCtrl();

		saleItemList = itemCtrl.getSaleItemList();

		panelSaleItem = new PanelTableModelAdapter(new SaleItemModel(saleItemList));

		panelSaleItem.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 2), "Vare p\u00E5 lager", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		
		panelSaleItem.getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		panelSaleItem.getTable().getSelectionModel()
				.addListSelectionListener(new ListSelectionListener()
				{
					@Override
					public void valueChanged(ListSelectionEvent event)
					{
						if (event.getValueIsAdjusting() == false)
						{
							indexTableSaleItem = panelSaleItem.getTable()
								.getSelectedRow();
						}
					}
				});
		
		
		JPanel panelDatoTid = new JPanel();
		panelDatoTid.setBorder(new LineBorder(new Color(0, 0, 0), 2));
		
		JPanel panelBrugeroplysninger = new JPanel();
		panelBrugeroplysninger.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 2), "Brugeroplysninger", TitledBorder.LEADING, TitledBorder.TOP, null, Color.BLACK));
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(panelSaleItem, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 545, Short.MAX_VALUE)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(rdbtnTraelast)
								.addComponent(rdbtnByggemarked)
								.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
									.addComponent(btnOpret, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(btnSlet, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addGroup(groupLayout.createSequentialGroup()
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
										.addComponent(btnUdskriv, GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
										.addComponent(btnRediger, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(panelBrugeroplysninger, GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE))
								.addComponent(panelDatoTid, GroupLayout.PREFERRED_SIZE, 178, GroupLayout.PREFERRED_SIZE))))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(rdbtnTraelast)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(rdbtnByggemarked)
							.addGap(5)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(btnOpret)
								.addComponent(btnRediger))
							.addGap(5)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(btnUdskriv)
								.addComponent(btnSlet)))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(panelDatoTid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(panelBrugeroplysninger, 0, 0, Short.MAX_VALUE)))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panelSaleItem, GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
					.addContainerGap())
		);
		
		JLabel lblBruger = new JLabel("Bruger: " );
		
		JLabel lblAfdeling = new JLabel("Afdeling: " );
		GroupLayout gl_panelBrugeroplysninger = new GroupLayout(panelBrugeroplysninger);
		gl_panelBrugeroplysninger.setHorizontalGroup
		(
			gl_panelBrugeroplysninger.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelBrugeroplysninger.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panelBrugeroplysninger.createParallelGroup(Alignment.LEADING)
						.addComponent(lblBruger)
						.addComponent(lblAfdeling))
					.addContainerGap(117, Short.MAX_VALUE))
		);
		gl_panelBrugeroplysninger.setVerticalGroup
		(
			gl_panelBrugeroplysninger.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelBrugeroplysninger.createSequentialGroup()
					.addComponent(lblBruger)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(lblAfdeling)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		panelBrugeroplysninger.setLayout(gl_panelBrugeroplysninger);
		
		CtrlLayer cl = new CtrlLayer();
		
		JLabel lblDate = new JLabel("Dato: " + cl.getDateString());
		panelDatoTid.add(lblDate);
		
		JLabel lblTid = new JLabel("Tid:");
		panelDatoTid.add(lblTid);
		
		lblTime1.setText(sdf.format(new Date(System.currentTimeMillis())));
		panelDatoTid.add(lblTime1);
		timer = new Timer(1000, this);
		timer.setRepeats(true);
		timer.start();
		setLayout(groupLayout);
		// TODO Auto-generated constructor stub	
	}
	
	public PanelLager(LayoutManager layout) 
	{
		super(layout);
		// TODO Auto-generated constructor stub
	}

	public PanelLager(boolean isDoubleBuffered) 
	{
		super(isDoubleBuffered);
		// TODO Auto-generated constructor stub
	}

	public PanelLager(LayoutManager layout, boolean isDoubleBuffered) 
	{
		super(layout, isDoubleBuffered);
		// TODO Auto-generated constructor stub
	}
	
	public void update()	
	{
		panelSaleItem.getTable().revalidate();
		panelSaleItem.getTable().repaint();
	}
	
	public void actionPerformed(ActionEvent e) 
	{
		// If the timer caused this event.
		if (e.getSource().equals(timer)) 
		{
			// Then set a new time.
			lblTime1.setText(sdf.format(new Date(System.currentTimeMillis())));
		}

	}
	
	public Clock main() {
		// TODO Auto-generated method stub
		return new Clock();
	}
}	

